//
//  VRCalendarTool.m
//  MyDemos
//
//  Created by Veer on 2016/11/2.
//  Copyright © 2016年 veer. All rights reserved.
//

#import "VRCalendarTool.h"

@implementation VRCalendarTool

#pragma mark 按周分
/** 获取本周第一天的日期 */
+ (NSDate *)dateForFistDayOnWeek:(NSDate *)date
{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2];
    
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:[calendar startOfDayForDate:date]];
    
    NSDateComponents *diffComponents = [[NSDateComponents alloc] init];
    diffComponents.day = -(components.weekday - 2);
    if (diffComponents.day == 1) {
        diffComponents.day = -6;
    }
    
    NSDate *firstDate = [calendar dateByAddingComponents:diffComponents toDate:[calendar startOfDayForDate:date] options:NSCalendarMatchStrictly];
    
    return firstDate;
}


/** 获取本周第一天的日期 */
+ (NSDate *)dateForLastDayOnWeek:(NSDate *)date
{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:1];
    
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:[calendar startOfDayForDate:date]];
    
    NSDateComponents *diffComponents = [[NSDateComponents alloc] init];
    diffComponents.day = (8 - components.weekday);
    if (diffComponents.day == 1) {
        diffComponents.day = 0;
    }
    
    
    NSDate *firstDate = [calendar dateByAddingComponents:diffComponents toDate:[calendar startOfDayForDate:date] options:NSCalendarMatchStrictly];
    
    return firstDate;
}


/** 获取上周一的日期 */
+ (NSDate *)datePreWeek:(NSDate *)date
{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:1];
    
    //    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
    //    [components setMonth:1];
    
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.day = -7;
    
    NSDate *preDate = [calendar dateByAddingComponents:dateComponents toDate:date options:NSCalendarMatchStrictly];
    
    return preDate;
}


/** 获取下周一的日期 */
+ (NSDate *)dateNextWeek:(NSDate *)date
{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:1];
    
    //    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
    //    [components setMonth:1];
    
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.day = 7;
    
    NSDate *nextDate = [calendar dateByAddingComponents:dateComponents toDate:date options:NSCalendarMatchStrictly];
    
    return nextDate;
}


#pragma mark 按月分
// 获取date当前月的第一天是星期几
+ (NSInteger)weekdayOfFirstDayWithDate:(NSDate *)date
{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:1];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
    [components setDay:1];
    NSDate *firstDate = [calendar dateFromComponents:components];
    NSDateComponents *firstComponents = [calendar components:NSCalendarUnitWeekday fromDate:firstDate];
    
    return firstComponents.weekday - 1;
}

// 获取date当前月的总天数
+ (NSInteger)totalDaysInMonthOfDate:(NSDate *)date
{
    
    NSRange range = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    return range.length;
}


// 获取上个月的日期
+ (NSDate *)preMonth:(NSDate *)date
{
    
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = -1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:NSCalendarMatchStrictly];
    return newDate;
}


// 获取下个月的日期
+ (NSDate *)nextMonth:(NSDate *)date
{
    
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = 1;
    NSDate *newDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:date options:NSCalendarMatchStrictly];
    return newDate;
}



+ (NSString *)stringWithDate:(NSDate *)date formatString:(NSString *)formatStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = formatStr;

    return [formatter stringFromDate:date];
}



@end
